#*******************************{begin:header}*******************************#
#                       rrnx - The Robust RINEX Library                      #
#****************************************************************************#
#
#       Part of the GPS/INS measurement simulation system GSIM,
#       https://code.google.com/p/gsim
#
#       Copyright (C) 2013-2014 Jani Hautamaki <jani.hautamaki@hotmail.com>
#
#       Licensed under the terms of GNU General Public License v3.
#
#       You should have received a copy of the GNU General Public License v3
#       along with this program as the file LICENSE.txt; if not, please see
#       http://www.gnu.org/licenses/gpl-3.0.html
#
#*********************************{end:header}*******************************#

#
# References:
#
# [1] GNU Make documentation:
#     http://www.gnu.org/software/make/manual/make.html
#

# These won't clean the make database much,
# but they'll disable the evaluation of the built-in rules.
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:

# Assign pre-defined variables explicitly
# C Compiler
CC=gcc
CFLAGS=-c -Wall --std=c99 -I../librrnx/include

# Linker
# gcc is actually used for doing the linking too,
# to get the proper defaultlibs included.
LD=gcc
LDLIBS=-lrrnx
LDFLAGS=-L $(LIBDIR)

# Archive-maintainer
AR=ar
ARFLAGS=


# Output directories
BUILDDIR=../../build
OBJDIR=$(BUILDDIR)/obj/tests
LIBDIR=$(BUILDDIR)/lib
TESTDIR=$(BUILDDIR)/tests
# Output files
LIBNAME=$(LIBDIR)/librrnx.a


# Input directory
SRCDIR=.


# Compute list of source code files
SOURCES=$(wildcard $(SRCDIR)/*.c)

# Compute list of objects that need to be compiled from the sources.
OBJS=$(patsubst $(SRCDIR)/%,$(OBJDIR)/%,$(SOURCES:.c=.o))
# The following could also work:
# $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

# Compute list of binaries that need to be built.
TESTBINS=$(patsubst $(SRCDIR)/%,$(TESTDIR)/%,$(SOURCES:.c=))


# targets which are not real
.PHONY: all clean

# Targets whose intermediate files are not to be deleted.
#.PRECIOUS: all
.SECONDARY: $(OBJS)

#############################################################################
# Default target

all: $(TESTBINS)

#############################################################################
# Pull in dependencies for the .o files that are already built.
# Enables the compiler to know when an existing .o file needs rebuild.
#
# From GNU make manual:
# "Suffix rules with no recipe are also meaningless. 
#  They do not remove previous rules as do pattern rules with 
#  no recipe (see Canceling Implicit Rules). They simply enter 
#  the suffix or pair of suffixes concatenated as 
#  a target in the data base. "

-include $(OBJS:.o=.d)

#############################################################################
# General pattern for test binaries
# The rule: each .o -> stand-alone binary.
# However, because each .o file is generated by another pattern,
# they are considered (for some strange reason!?) as intermediate files,
# and are treated differentely than other prerequisities, ie. they get
# deleted after the build, so we need the target rules to be PRECIOUS
#
# Each test binary is linked again, if the library has been updated.

$(TESTDIR)/%: $(OBJDIR)/%.o $(LIBNAME) | $(TESTDIR)
	$(LD) $(LDFLAGS) $^ $(LDLIBS) -o $@

#############################################################################
# General pattern for objects:
#   1) Compile the file,
#   2) Calculate dependencies for the next run.
#
# Note:
#   "Note that .d files exist if and only if the corresponding .o file exists. 
#    This makes sense, since if the .o file doesn't exist yet, 
#    we don't need a .d file to tell us it has to be rebuilt."
#    -from http://scottmcpeak.com/autodepend/autodepend.html
#
# Special symbols used here:
#   $<    is the source file
#   $@    is the target file
#

$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
	$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
	@$(CC) $(CFLAGS) -MM -MT"$@" $< -MF"$(patsubst %.o,%.d,$@)"
	

# Directory creation; an order-only prerequisite
$(OBJDIR):
	-mkdir $(OBJDIR)

$(LIBDIR):
	-mkdir $(LIBDIR)

$(TESTDIR):
	-mkdir $(TESTDIR)

# Remove compilation products
clean:
	rm -rf $(OBJDIR) $(TESTDIR)


# http://stackoverflow.com/questions/97338/gcc-dependency-generation-for-a-different-output-directory
# automatically generate dependency rules
#%.d : %.c
#	$(CC) $(CFLAGS) -MF"$@" -MG -MM -MP -MT"$@" -MT"$(<:.c=.o)" "$<"
# -MF  write the generated dependency rule to a file
# -MG  assume missing headers will be generated and don't stop with an error
# -MM  generate dependency rule for prerequisite, skipping system headers
# -MP  add phony target for each header to prevent errors when header is missing
# -MT  add a target to the generated dependency

